home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
mg2a_src.zip
/
RE_SEARC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-08-23
|
19KB
|
707 lines
/*
* regular expression search commands for
* MicroGnuEmacs
*
* This file contains functions to implement several of gnuemacs'
* regular expression functions for MicroGnuEmacs. Several of
* the routines below are just minor rearrangements of the MicroGnuEmacs
* non-regular expression search functions. Hence some of them date back
* in essential structure to the original MicroEMACS; others are modifications
* of Rich Ellison's code. I, Peter Newton, wrote about half from scratch.
*
* Although I have nothing to do with the GNU project, these functions
* require the GNU project's regular expression package (files regex.c and
* regex.h). Hence, this file comes under the same copyright notice
* as the GNU project's code. As far as I know, the rest of MicroGnuEmacs
* need not since it may be used independently of any GNU project code. In
* any case, I certainly do not warrant either the correctness or utility
* of this code. The GNU project copyright notice follows. Don't you
* wish they would make it a bit shorter!
*/
/*
GNU Emacs copying permission notice Copyright (C) 1985 Richard M. Stallman
Verbatim copies of this document, including its copyright notice,
may be distributed by anyone in any manner.
Distribution with modifications is not permitted.
GNU Emacs is distributed in the hope that it will be useful,
but without any warranty. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.
Everyone is granted permission to copy, modify and redistribute
GNU Emacs under the following conditions:
Permission is granted to anyone to make or distribute verbatim copies
of GNU Emacs source code as received, in any medium, provided that all
copyright notices and permission and nonwarranty notices are preserved,
and that the distributor grants the recipient permission
for further redistribution as permitted by this document,
and gives him and points out to him an exact copy of this document
to inform him of his rights.
Permission is granted to distribute modified versions
of GNU Emacs source code, or of portions of it,
under the above conditions, provided also that all
changed files carry prominent notices stating who last changed them
and that all the GNU-Emacs-derived material, including everything
packaged together with it and not independently usable, is
distributed under the conditions stated in this document.
Permission is granted to distribute GNU Emacs in
compiled or executable form under the same conditions applying
for source code, provided that either
A. it is accompanied by the corresponding machine-readable
source code, or
B. it is accompanied by a written offer, with no time limit,
to give anyone a machine-readable copy of the corresponding
source code in return for reimbursement of the cost of distribution.
This written offer must permit verbatim duplication by anyone.
C. it is distributed by someone who received only the
executable form, and is accompanied by a copy of the
written offer of source code which he received along with it.
In other words, you are welcome to use, share and improve GNU Emacs
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding!
*/
#ifdef REGEX
#include "def.h"
#include "macro.h"
#define SRCH_BEGIN (0) /* Search sub-codes. */
#define SRCH_FORW (-1)
#define SRCH_BACK (-2)
#define SRCH_NOPR (-3)
#define SRCH_ACCM (-4)
#define SRCH_MARK (-5)
char re_pat[NPAT]; /* Regex pattern */
int re_srch_lastdir = SRCH_NOPR; /* Last search flags. */
int casefoldsearch = TRUE; /* Does search ignore case ? */
/* Indexed by a character, gives the upper case equivalent of the character */
static char upcase[0400] =
{ 000, 001, 002, 003, 004, 005, 006, 007,
010, 011, 012, 013, 014, 015, 016, 017,
020, 021, 022, 023, 024, 025, 026, 027,
030, 031, 032, 033, 034, 035, 036, 037,
040, 041, 042, 043, 044, 045, 046, 047,
050, 051, 052, 053, 054, 055, 056, 057,
060, 061, 062, 063, 064, 065, 066, 067,
070, 071, 072, 073, 074, 075, 076, 077,
0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
};
/*
* Search forward.
* Get a search string from the user, and search for it,
* starting at ".". If found, "." gets moved to just after the
* matched characters, and display does all the hard stuff.
* If not found, it just prints a message.
*/
/*ARGSUSED*/
re_forwsearch(f, n) {
register int s;
if ((s=re_readpattern("RE Search")) != TRUE)
return (s);
if (re_forwsrch() == FALSE) {
ewprintf("Search failed: \"%s\"", re_pat);
return (FALSE);
}
re_srch_lastdir = SRCH_FORW;
return (TRUE);
}
/*
* Reverse search.
* Get a search string from the user, and search, starting at "."
* and proceeding toward the front of the buffer. If found "." is left
* pointing at the first character of the pattern [the last character that
* was matched].
*/
/*ARGSUSED*/
re_backsearch(f, n) {
register int s;
if ((s=re_readpattern("RE Search backward")) != TRUE)
return (s);
if (re_backsrch() == FALSE) {
ewprintf("Search failed: \"%s\"", re_pat);
return (FALSE);
}
re_srch_lastdir = SRCH_BACK;
return (TRUE);
}
/*
* Search again, using the same search string
* and direction as the last search command. The direction
* has been saved in "srch_lastdir", so you know which way
* to go.
*/
/*ARGSUSED*/
/* This code has problems-- some incompatibility(?) with
extend.c causes match to fail when it should not.
*/
re_searchagain(f, n) {
if (re_srch_lastdir == SRCH_NOPR) {
ewprintf("No last search");
return (FALSE);
}
if (re_srch_lastdir == SRCH_FORW) {
if (re_forwsrch() == FALSE) {
ewprintf("Search failed: \"%s\"", re_pat);
return (FALSE);
}
return (TRUE);
}
if (re_srch_lastdir == SRCH_BACK) {
if (re_backsrch() == FALSE) {
ewprintf("Search failed: \"%s\"", re_pat);
return (FALSE);
}
return (TRUE);
}
}
#include "regex.h"
#define BYTEWIDTH 8
/* Compiled regex goes here-- changed only when new pattern read */
static struct re_pattern_buffer re_buff;
static char fastmap[(1 << BYTEWIDTH)];
/* regs holds boundaries of matched text */
static struct re_registers regs;
/*
* Re-Query Replace.
* Replace strings selectively. Does a search and replace operation.
*/
/*ARGSUSED*/
re_queryrepl(f, n) {
register int s;
register int rcnt = 0; /* Replacements made so far */
register int plen; /* length of found string */
char news[NPAT]; /* replacement string */
/* Casefold check */
if (!case